<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
    :append-to-body="true"
  >
    <el-row>
      <el-form
        label-width="80px"
        ref="createForm"
        :rules="rules"
        :model="formData"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="formData.userName" />
        </el-form-item>
        <el-form-item label="邮箱" prop="emailAddress">
          <el-input v-model="formData.emailAddress" />
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="formData.id == 0">
          <el-input v-model="formData.password" type="password" />
        </el-form-item>
        <el-form-item label="是否激活">
          <el-tooltip
            class="item"
            effect="dark"
            content="激活后用户才能登录"
            placement="right"
          >
            <el-checkbox v-model="formData.isActive"></el-checkbox>
          </el-tooltip>
        </el-form-item>
        <el-form-item label="角色">
          <el-checkbox-group v-model="formData.roleNames">
            <el-checkbox
              v-for="(item, index) in roleData"
              :label="item.normalizedName"
              :key="index"
              >{{ item.displayName }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose()">取 消</el-button>
      <el-button type="primary" @click="comit()" :loading="loading"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { GetUser, GetRoles, Create, Update } from '@api/systemManage/userManage'
export default {
  data () {
    return {
      title: '',
      dialogVisible: false,
      permissionsData: [],
      loading: false,
      formData: {
        emailAddress: '',
        id: 0,
        isStatic: false,
        name: '',
        roleNames: [],
        isActive: true,
        surname: '',
        userName: ''
      },
      roleData: [],
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        emailAddress: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }

    }
  },
  created () {

  },
  methods: {
    comit () {
      this.$refs.createForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.formData.id === 0) {
            this.createUser()
          } else {
            this.updateUser()
          }
        }
      })
    },
    createUser () {
      this.formData.surname = this.formData.name
      Create(this.formData).then(res => {
        this.loading = false
        if (res.success) {
          this.handleClose()
          this.$notify({
            title: '提示',
            message: '添加成功',
            type: 'success'
          })
        }
      })
    },
    updateUser () {
      Update(this.formData).then(res => {
        this.loading = false
        if (res.success) {
          this.handleClose()
          this.$notify({
            title: '提示',
            message: '修改成功',
            type: 'success'
          })
        }
      })
    },
    show (value, title) {
      if (value !== '') {
        this.getuser(value)
      } else {
        this.getRole()
      }
      this.title = title
    },
    getuser (userId) {
      GetUser(userId).then(res => {
        if (res.success) {
          this.formData = res.result
          this.getRole()
        }
      })
    },
    getRole () {
      GetRoles().then(res => {
        if (res.success) {
          this.roleData = res.result.items
          this.dialogVisible = true
        }
      })
    },
    handleClose () {
      this.formData = {
        emailAddress: '',
        id: 0,
        isStatic: false,
        name: '',
        roleNames: [],
        isActive: true,
        surname: '',
        userName: ''
      }
      this.dialogVisible = false
      this.$emit('fromvisible', false)
    }
  }
}
</script>
